﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using System.IO;
using NUnit.Framework;
using COFE.Core.Utils;
using System.Text.RegularExpressions;
using System.IO.IsolatedStorage;
using COFE.Core.Profile;
using COFE.Core.Entry;

namespace COFE.IO.ISG
{
    public class IsolatedStorageDirectoryLister : PrimaryDirectoryListerBase<ISGEntryInfo>, IPrimaryDirectoryLister2
    {
        #region Constructors
  
        public IsolatedStorageDirectoryLister(IFactory<IDirectoryLister> factory, string listerKey, IsolatedStorageFile storageFile)
            : base(factory, listerKey)
        {
            Assert.IsNotNull(storageFile);
            _storageFile = storageFile;

            //Directory Lister
            _list = (item) => { return item.EnumerateSubEntries(); };
            _isDirectory = (item) => { return item.IsFolder; };
            _getParent = (item) =>
                {
                    return item.GetParent();
                };


            //Primar Directory Lister
            _parse = (path) =>
            {
                try
                {
                    if (String.IsNullOrEmpty(path)) return new ISGEntryInfo(storageFile, "", true); //Root

                    string parentPath = Path.GetDirectoryName(path);
                    string entryName = Path.GetFileName(path);

                    if (storageFile.DirectoryExists(parentPath))
                    {
                        foreach (var dir in storageFile.GetDirectoryNames(parentPath + "\\*"))
                            if (dir.Equals(entryName, StringComparison.CurrentCultureIgnoreCase))
                                return new ISGEntryInfo(storageFile, path, true);

                        foreach (var file in storageFile.GetFileNames(parentPath + "\\*"))
                            if (file.Equals(entryName, StringComparison.CurrentCultureIgnoreCase))
                                return new ISGEntryInfo(storageFile, path, false);

                        //Return as folder.
                        return new ISGEntryInfo(storageFile, path, true);
                    }
                    else return null;

                    
                }
                catch { }                
                return null;
            };            

            _getTempPath = () => { return Path.GetTempPath(); };
            _getRootDirectory = () => { return new ISGEntryInfo[] { new ISGEntryInfo(storageFile, "", true) }; };
            _getSupportedPathPatterns = () => { return new string[] { RegexPatterns.RelativePathPattern }; };

        }

        #endregion

        #region Methods


        public bool Exists(string path, out bool isFolder)
        {
            var parsedItem = _parse(path);
            if (parsedItem != null)
            {
                isFolder = parsedItem.IsFolder;
                return parsedItem.IsExists;
            }
            
            isFolder = false;
            return false;
        }

        public override IStreamProvider CreateStreamProvider()
        {
            return new IsolatedStorageStreamProvider(this);
        }

        public override IDirectoryController CreateController()
        {
            return new IsolatedStorageDirectoryController(this);
        }

        public override IPropertyProvider CreatePropertyProvider(ISGEntryInfo item)
        {
            return new IsolatedStoragePropertyProvider(ListerKey, item);
        }


        #endregion


        #region Data

        private IsolatedStorageFile _storageFile;

        #endregion

        #region Public Properties

        public IsolatedStorageFile IsolatedStorageFile { get { return _storageFile; } }        

        #endregion

    }
}
